<template>
  <div>
    <el-table :data="list" style="width: 100%">
      <el-table-column label="排序" width="180">
        <template slot-scope="scope">
          <div>
            <p class="d-flex">
              <el-input
                v-model="scope.row.sort"
                :disabled="true"
                size="medium"
                class="w-40 input_sort mr"
                @blur="handleBlur(scope, 'input_sort')"
              />
              <i class="el-icon-edit fs" @click.stop="iconClick(scope.$index, 'input_sort')" />
            </p>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="图片">
        <template slot-scope="scope">
          <div>
            <my-upload :width="140" :height="80" :img="scope.row.image" @remove="handleImgRemove(scope)" @success="(image) => {handleSuccess(scope, image)}" />
          </div>
        </template>
      </el-table-column>
      <el-table-column label="链接">
        <template slot-scope="scope">
          <div>
            <p class="d-flex">
              <el-input
                v-model="scope.row.link"
                size="medium"
                :disabled="true"
                class="w-40 input_link mr"
                @blur="handleBlur(scope, 'input_link')"
              />
              <i class="el-icon-edit fs" @click.stop="iconClick(scope.$index, 'input_link')" />
            </p>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <div>
            <span class="col-red" @click="carouselDelete(scope.row)">删除</span>
          </div>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>
<script>
import MyUpload from '@/common/my-upload'
import { deleteCarouselItem } from '@/api/http/carousel'
const ERR_OK = 200
export default {
  components: {
    MyUpload
  },
  props: {
    list: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      tableData: [
        {
          date: '2016-05-02',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1518 弄'
        }
      ]
    }
  },
  methods: {
    handleSuccess(scope, image) {
      this.$emit('image', image, scope.row)
    },
    handleImgRemove(scope) {
      scope.row.image = ''
    },
    iconClick(index, name) {
      const el = document.querySelectorAll(`.${name}`)[index].children[0]
      el.style.background = '#eeee'
      el.removeAttribute('disabled')
      el.focus()
    },
    handleBlur(scope, name) {
      const index = scope.$index
      const row = scope.row
      const el = document.querySelectorAll(`.${name}`)[index].children[0]
      el.style.background = 'transparent'
      el.setAttribute('disabled', 'disabled')
      switch (name) {
        case 'input_sort':
          this.$emit('sort', row)
          break
        case 'input_link':
          this.$emit('link', row)
          break
      }
    },
    carouselDelete(row) {
      this.$confirm('确认删除？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this._deleteCarouselItem(row.id).then(res => {
            if (res.code === ERR_OK) {
              this.$emit('success')
              this.$message({
                type: 'success',
                message: '删除成功!',
                showClose: true
              })
            }
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    _deleteCarouselItem(id) {
      return deleteCarouselItem(id)
        .then(res => {
          if (res.code === ERR_OK) {
            console.log(res)
            return Promise.resolve(res)
          }
        })
        .catch(err => {
          console.log(err)
        })
    }
  }
}
</script>
<style lang="scss" scoped>
@import "@/styles/color.scss";
@import "@/styles/el-table.scss";
.fs {
  font-size: 20px;
  color: #409eff;
  cursor: pointer;
  opacity: 0;
  transform: all 2s;
}
.el-table /deep/ .el-table__row:hover {
  .fs {
    opacity: 1;
  }
}
.w-40 {
  width: 40px;
}
.d-flex {
  display: flex;
  align-items: center;
  justify-content: center;
}
.mr {
  margin-right: 10px;
}
.input_sort {
  width: 50%;
}
.input_link {
  width: 100%;
}
.el-input /deep/ .el-input__inner {
  background-color: transparent;
  border-color: transparent;
  color: #666666;
  border: none;
  height: auto;
  padding: 0;
  text-align: center;
  white-space: nowrap;
  cursor: pointer;
}
</style>
